﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade.enums;
using BLFacade;

namespace ProjectUI
{
    public partial class TaskForm : Form
    {
        private ITask task;
        private IProject project;
        public TaskForm()
        {
            InitializeComponent();
            lblStatus.Visible = false;
            lblStatusValue.Visible = false;
            gridviewExecuting.Visible = false;
            this.Text = "Task";
        }

        public TaskForm(ITask task)
        {
            InitializeComponent();
            this.Text = String.Format("Information about {0} task", task.Name);
            this.task = task;
            grbxInfo.Enabled = false;
            txtName.Text = task.Name;
            numericLOE.Value = task.LOE;
            lblCreatingDateValue.Text = task.CreateDate.ToString();
            if (task.CloseDate.HasValue) lblClosingDateValue.Text = task.CloseDate.Value.ToString();
            else
            {
                lblClosingDate.Visible = false;
                lblClosingDateValue.Visible = false;
            }
            cmbxEmployees.DataSource = FacadeHolder.DataFacade.GetEmployees();
            cmbxEmployees.SelectedValue = task.Employee.FullName;
            cmbxPriority.DataSource = Enum.GetValues(typeof(TaskPriority));
            lblStatusValue.Text = task.Status.ToString();
            txtDescription.Text = task.Description;
            btnCreate.Enabled = false;
            btnCreate.Text = "Submit";
            for (int i = 0; i < task.StartDates.Count; i++)
            {
                gridviewExecuting.Rows.Add();
                gridviewExecuting.Rows[i].Cells[0].Value = task.StartDates[i].ToString();
                if (task.ResolveDates[i].HasValue) gridviewExecuting.Rows[i].Cells[1].Value = task.ResolveDates[i].Value.ToString();
            }
            if (task.Status == TaskStatus.Closed || task.Status == TaskStatus.Canceled)
            {
                flowPanelButtons.Enabled = false;
            }
            if (FacadeHolder.UserFacade.CurrentPerson.Equals(task.Employee))
            {
                btnCreate.Visible = false;
                btnEdit.Visible = false;
                btnClose.Visible = false;
                if (task.Status == TaskStatus.Opened || task.Status == TaskStatus.Reopened) btnStartResolveReopen.Text = "Start";
                if (task.Status == TaskStatus.Resolved) btnStartResolveReopen.Enabled = false;
                if (task.Status == TaskStatus.InProgress) btnStartResolveReopen.Text = "Resolve";
            }
            if (FacadeHolder.UserFacade.CurrentPerson.Equals(task.Project.ProjectManager))
            {
                if (task.Status == TaskStatus.Resolved)
                {
                    btnStartResolveReopen.Text = "Reopen";
                    btnStartResolveReopen.Enabled = true;
                }
                else
                {
                    btnClose.Text = "Cancel";
                    btnStartResolveReopen.Enabled = false;
                }
            }
        }

        public TaskForm(IProject project)
        {
            InitializeComponent();
            this.project = project;
            cmbxEmployees.DataSource = FacadeHolder.DataFacade.GetEmployees();
            cmbxPriority.DataSource = Enum.GetValues(typeof(TaskPriority));
            gridviewExecuting.Visible = false;
            btnEdit.Visible = false;
            btnStartResolveReopen.Visible = false;
            btnClose.Visible = false;
            lblCreatingDate.Visible = false;
            lblCreatingDateValue.Visible = false;
            lblClosingDate.Visible = false;
            lblClosingDateValue.Visible = false;
            lblStatus.Visible = false;
            lblStatusValue.Visible = false;
            this.Text = String.Format("New task in {0}", project.Name);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnCreate.Enabled = grbxInfo.Enabled = !grbxInfo.Enabled;
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtName.Text))
            {
                errorFormProvider.SetError(txtName, "This field mustn't be empty");
            }
            else
            {
                errorFormProvider.SetError(txtName, "");
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(errorFormProvider.GetError(txtName))) return;
            if (project != null)
            {
                project.CreateTask(txtName.Text, txtDescription.Text, (int)numericLOE.Value, (int)cmbxPriority.SelectedItem, (IEmployee)cmbxEmployees.SelectedItem);
            }
            else if (task != null)
            {
                task.Name = txtName.Text;
                task.Description = txtDescription.Text;
                task.LOE = (int)numericLOE.Value;
                task.Employee = (IEmployee)cmbxEmployees.SelectedItem;
                task.Priority = (TaskPriority)cmbxPriority.SelectedItem;
            }
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }

        private void btnStartResolveReopen_Click(object sender, EventArgs e)
        {
            switch (task.Status)
            {
                case TaskStatus.Opened: task.Start(); break;
                case TaskStatus.InProgress: task.Resolve(); break;
                case TaskStatus.Resolved: task.Reopen(); break;
                case TaskStatus.Reopened: task.Start(); break;
                default: break;
            }
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            task.Close(btnClose.Text.Equals("Close"));
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }
    }
}
